<?php
class gladiator_animals
{

  function __construct($user_info,$user_id) {
       //print "In constructor\n";
       $this->user_info = $user_info;
       $this->user_id = $user_id;
   }


function show_all_animals()
{
global $db; 
                $sql = "SELECT * FROM `gladiator_animals`";
                $animal_info = $db->fetch_real_array($sql);
                
                $options = array();
                foreach ($animal_info as $key => $value)
                {
                            $animal_name = $value['breed'];
                            $pre = '';
                            $post = '';
                            $options["/gladiator/fight/animals/$animal_name/"] = ''.$pre.$value['breed'].$post.'';											
                }
                
                //$options['/gladiator/'] = '<b>Take me back to the main page</b>';
                echo player_options($options);

}

function check_alive ($user_id,$energy)
{
	if ($energy > 0)
	{
		return true;
	}
	else 
	{
		return false;
	}
}

function show_attack($user_info,$breed,$energy_used_percent,$area_hit,$distance_kept)
{
global $db;

#
# Battle
# 
# 1. Must check if player attacking is alive and has enough energy
# 2. Calculate the speeds of both opponents against each other
# 3. Calculate the damage done by the fastest person
#
#
#
#
#
#

$output = '';

# Load the battle from the database (checks later to see if its a new battle or not)
$battle_info_sql = "SELECT * FROM `user_battle_animal` join `gladiator_animals` using ( breed ) where assoc_id = '$this->user_id'";
$battle_info = $db->return_first_result($battle_info_sql);

# check to see if this is the first hit or not
if (empty($battle_info))
{
	# this is the first hit so add the new battle into the database
	$animal_energy = 1000;
	$sql = "INSERT INTO `user_battle_animal` 
		( `assoc_id` , `breed` , `animal_energy` , `last_hit` , `last_energy_used` )
		VALUES (
		'$this->user_id', '$breed', '$animal_energy', '', ''
		)";
	$db->query($sql);
	$battle_info = $db->return_first_result($battle_info_sql);
}

$level = $battle_info['level'];

// Check YOU (USER_ID) are alive
$alive_check_user = $this->check_alive(USER_ID,$user_info['energy']);
$alive_check_animal = $this->check_alive(USER_ID,$battle_info['animal_energy']);

if ($alive_check_user && $alive_check_animal)
{
	# both person and animal are alive
	
	$energy = $user_info['energy'];
	
	$energy_used_total = $energy * ($energy_used_percent/100);
	
	$force = $energy_used_total + $this->calculate_weapon_bonus($user_info['equip_weapon']);
	
	$user_speed = $user_info['speed'];
	
	$animal_speed = $level;
	$animal_speed = $this->calculate_reaction_bonus($animal_speed,$area_hit);
	
	$user_battle = array('speed' => $user_speed, 'force' =>$force, 'area_hit' => $area_hit);
	$animal_battle = array('speed' => $animal_speed, 'breed' => $breed, 'level' => $level);
	
	$battle_result = $this->battle($battle_info,$user_battle,$animal_battle);
	
	echo $battle_result;
	
	
	$output .= "animal speed = $animal_speed; user_speed = $user_speed<br>";
	$output .= "breed: $breed; energy: $energy_used_total, area: $area_hit";
	
}
else 
{
	$output = 'someone is dead ';
	$this->end_battle();
}

return $output;

}

function calculate_weapon_bonus($weapon_id)
{
	global $db;
	# TODO This function should take the weapon ID, look it up and then return the amount of points add it adds on to a persons hit
	$sql = "SELECT * FROM `gladiator_items` WHERE `id` = '$weapon_id'";
	$weapon_info = $db->return_first_result($sql);
	return $weapon_info['power'];
}

function battle ($battle_info,$user_battle,$animal_battle,$limit = 0)
{
	global $db;
	
	$breed = $animal_battle['breed'];
	
	$output = '';

	if ($user_battle['speed'] > $animal_battle['speed'])
	{
		# person hits the animal
		display_var(get_defined_vars());
		
		# how much energy to be deducted from the animal
		$force = $user_battle['force'];
		
		if ($force > $battle_info['animal_energy'])
		{
			# delete the record from the database
			$this->end_battle();
			$this->add_win($this->user_id);
			$this->add_loss($breed);
			$output = "<BR><BR><B>You killed the $breed</b><BR><BR>";
		}
		else 
		{
			# deduct the energy from the animal
			$sql = "UPDATE `user_battle_animal` SET `animal_energy`=animal_energy - '$force' WHERE `assoc_id`='$this->user_id'";
			$db->query($sql);
		}
		
		
		$output .= "You hit the $breed with $force force";
		
	}
	elseif ($user_battle['speed'] < $animal_battle['speed']) 
	{
		# animal hits the person
	}
	else 
	{
		# Battle Again 
		if ($limit < 3)
		{
			$this->battle($user_battle,$animal_battle,$limit++);
		}
		else 
		{
			return false;
		}
	}
	return $output;
}

function end_battle()
{
	global $db;
	$sql = "DELETE FROM `user_battle_animal` WHERE `assoc_id`='$this->user_id'";
	$db->query($sql);
}

function add_win($who)
{
	global $db;
	
	
	if (is_numeric($who))
	{
		# winner is human
		$sql = "UPDATE `user_gladiator` SET `animal_wins`=animal_wins+'1' WHERE `assoc_id`='$who'";
	}
	elseif (is_string($who))
	{
		# winner is an animal
		$sql = "UPDATE `gladiator_animals` SET `wins`=wins+'1' WHERE `breed`='$who'";
	}
	$result = $db->query($sql);
	
	return $result;
}

function add_loss($who)
{
	global $db;
	
	if (is_numeric($who))
	{
		# loser is an animal
		$sql = "UPDATE `user_gladiator` SET `animal_losses`=animal_losses+'1' WHERE `assoc_id`='$who'";
	}
	elseif (is_string($who))
	{
		# loser is human
		$sql = "UPDATE `gladiator_animals` SET `losses`=losses+'1' WHERE `breed`='$who'";
	}
	$result = $db->query($sql);
	
	return $result;
}

function calculate_reaction_bonus($speed,$area_hit)
{
	switch ($area_hit)
	{
		case 'head':
			$reaction_bonus = 3;
			break;
		case 'torso':
			$reaction_bonus = 1.5;
			break;
		case 'limbs':
			$reaction_bonus = 1.5;
			break;
		case 'other':
			$reaction_bonus = 1;
			break;
		default:
			$reaction_bonus = 1;
	}
	
	return $speed * $reaction_bonus;

}

function show_attack_options($breed,$energy=20,$area=0,$distance=0)
{
        # Attack-bar
        #
        # To show all available attack options
        #    (something like...)
        #
        # Where do you want to attack?
        # [Head][Torso][Legs]
        #
        # Tactical Options
        # [Distance yourself away][Surrender]
        
        $output = '';

            // Include the javascript reference
            $output .=  '<script language="JavaScript" src="/slider.js"></script>';
            
            
            // include the form element
            $output .=  '<form action="'.URL_SELF.'" method="post" name="">';
            $output .=  '<input name="energy" id="sliderValue1h" type="hidden" size="3" onchange="A_SLIDERS[0].f_setValue(this.value)">';
            
            $output .=  '<style type="text/css">
            table.sample {
                border-width: 1px 1px 1px 1px;
                border-spacing: 2px;
                border-style: none none none none;
                border-color: gray gray gray gray;
                border-collapse: separate;
                background-color: rgb(216, 198, 166);
            }
            table.sample th {
                border-width: 1px 1px 1px 1px;
                padding: 1px 1px 1px 1px;
                border-style: solid solid solid solid;
                border-color: gray gray gray gray;
                background-color: rgb(216, 198, 166);
                -moz-border-radius: 0px 0px 0px 0px;
            }
            table.sample td {
                border-width: 1px 1px 1px 1px;
                padding: 1px 1px 1px 1px;
                border-style: solid solid solid solid;
                border-color: gray gray gray gray;
                background-color: rgb(216, 198, 166);
                -moz-border-radius: 0px 0px 0px 0px;
            }
            </style><table class="sample" cellpadding=10 cellspacing=20><TR><TD colspan=2 align=center><b>Attack Bar </b></TD></TR><tr><td>Select an area to attack:</td><td>';
            if ($area == 'head'){ $checked = ' checked'; } else { $checked = ''; }
            $output .= "<input type=radio name=area value=head $checked> Head";
            if ($area == 'torso'){ $checked = ' checked'; } else { $checked = ''; }
            $output .=  "<BR><input type=radio name=area value=torso $checked> Torso";
            if ($area == 'limbs'){ $checked = ' checked'; } else { $checked = ''; }
            $output .=  "<BR><input type=radio name=area value=limbs $checked> Limbs";
            $output .=  '</td>';
            
            $output .=  '<tr><td>Keeping a distance of: </td>';
            $output .=  '<td><select name=distance>
            <option> 1 meter</option>
            <option> 2 meters</option>
            <option> 3 meters</option>
            <option> 4 meters</option>
            </select></td>';
            
            // Include the actual slider
            $output .=  "<tr><td>Use Energy (%): <BR><Small>(drag pointer to desired amount)</small></td> <td><script language=\"JavaScript\">
                var A_TPL1h = {
                    'b_vertical' : false,
                    'b_watch': true,
                    'n_controlWidth': 120,
                    'n_controlHeight': 16,
                    'n_sliderWidth': 16,
                    'n_sliderHeight': 15,
                    'n_pathLeft' : 1,
                    'n_pathTop' : 1,
                    'n_pathLength' : 103,
                    's_imgControl': '/img/sldr2h_bg.gif',
                    's_imgSlider': '/img/blueh_sl.gif',
                    'n_zIndex': 1
                }
            
                var A_INIT1h = {
                    's_form' : 0,
                    's_name': 'sliderValue1h',
                    'n_minValue' : 0,
                    'n_maxValue' : 50,
                    'n_value' : $energy,
                    'n_step' : 1
                }
                new slider(A_INIT1h, A_TPL1h);
            </script></td></tr>";
            
            $output .= "<TR><TD colspan=2 align=center><input type=submit value=\"Attack!\"></TD></TR></table>";
            
            
            $output .=  '</form>';
                
        return $output;
}

}

?>